/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package r.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @author jcrada
 */
public class Datatype {

    public static String C(List<?> x, String begin_enclosing, String end_enclosing) {
        String c = "c(";
        for (int i = 0; i < x.size(); ++i) {
            c += begin_enclosing + x.get(i).toString() + end_enclosing;
            if (i < x.size() - 1) {
                c += ", ";
            }
        }
        c += ")";
        return c;
    }

    public static String C(List<?> x, String enclosing) {
        return C(x, enclosing, enclosing);
    }

    public static String C(List<?> x) {
        return C(x, "");
    }

    public static String SquareMatrix(List<String> names) {
        return "matrix(NA," + names.size() + "," + names.size() + ","
                + "dimnames=list(" + C(names, "'") + "))";
    }

    public static String DataFrame(List<?> names, List<?>... cols) {
        String result = "";

        result += "data.frame(";
        for (int i = 0; i < names.size(); ++i) {
            result += names.get(i).toString() + "=" + C(cols[i]);
            if (i < names.size() - 1) {
                result += ", ";
            }
        }
        result += ")";
        return result;
    }

    public static String DataFrame(Map<String, List<String>> map) {
        return DataFrame(map,"");
    }
    
    public static String DataFrame(Map<String, List<String>> map, String props) {
        String result = "";
        result += "data.frame(";

        for (Iterator it = map.keySet().iterator(); it.hasNext(); ) {
            
            Object key = it.next();
            result += key.toString() + "=" + C(map.get(key));
            if (it.hasNext()) {
                result += ", ";
            }
        }
        if (props != null && !props.isEmpty()){
            result += ", " +  props;
        }
        result += ")";
        return result;
    }
}
